home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / makecorr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  11.9 KB  |  503 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "ada.h"
  10. #include "adalexprots.h"
  11. #include "pspansprots.h"
  12. #include "prsutilprots.h"
  13. #include "errsprots.h"
  14. #include "lookupprots.h"
  15. #include "recoverprots.h"
  16. #include "makecorrprots.h"
  17.  
  18. static void reset_cands();
  19.  
  20. struct    tok_loc PRSERR_LOC ;
  21.  
  22.  
  23. void make_correction(struct two_pool *STATE_STACK)        /*;make_correction*/
  24. {
  25.     /* Choose a correction by trying the modes in the order: merge, spelling,
  26.      * insertion, deletion, substitution.  
  27.      * Generate appropriate error message.
  28.      * Apply corrective action to token sequence, and restore state and parse
  29.      * stacks to correction point.
  30.      */
  31.     struct    prsstack *ct,*nt,*r,*tokmod,*curtoken ;
  32.     struct    two_pool *tmp_tp ;
  33.     int    i ;
  34.     char    *tv,*ctv,*ntv ;
  35.     int        intok,subtk,bk ;
  36.     int        n_STATE_STACK ;
  37.     char    msg[200];
  38.     int    delete_flag = 0;
  39.  
  40.     /************************** M E R G E *********************************/
  41.  
  42.     if ( CANDIDATES[MERGE] != NULL ) {
  43.  
  44.         /* [subtk, bk] := arb CANDIDATES[MERGE];                            
  45.          * This will be achieved by taking the first element of the list    
  46.          */
  47.  
  48.         tv = namelist( subtk = CANDIDATES[MERGE]->token_sym ) ;
  49.  
  50.         /*    ct = bk == 0 ? curtok : PREVTOK ; */
  51.  
  52.         if((bk = CANDIDATES[MERGE]->backup_toks) == 0)
  53.             ct = copytoken(curtok) ;
  54.         else
  55.             ct = copytoken(PREVTOK) ;
  56.  
  57.         /*     nt =  bk == 0 ? nexttok : curtok ; */
  58.         if(bk == 0)
  59.             nt = copytoken(nexttok) ;
  60.         else
  61.             nt = copytoken(curtok) ;
  62.  
  63.         ctv = namelist(ct->ptr.token->index);
  64.         ntv = namelist(nt->ptr.token->index);
  65.  
  66.         /* PRSERR_LOC = position(ct); */
  67.         PRSERR_LOC = ct->ptr.token->loc ;
  68.         sprintf(msg,"\"%s\" expected instead of \"%s\" \"%s\"",
  69.           tv,ctv,ntv) ;
  70.         syntax_err(LLOC(ct),RLOC(nt),msg);
  71.  
  72.         for (i = 1 ; i <= bk ; i++) {
  73.             r = prs_stack ;
  74.             prs_stack = prs_stack->prev ;
  75.             ADDTOTOP(r) ;
  76.         } 
  77.  
  78.         /*
  79.          * tokens[tokind - 1 ] = [[subtk, namemap(token_value_des(tv) ? tv),
  80.          *                        top(ct)]];
  81.          */
  82.          TOKMINUS ;
  83.          tokens[tokind]->symbol = subtk ;
  84.          tokens[tokind]->ptr.token->index = namemap(tv,strlen(tv));
  85.          tokens[tokind]->ptr.token->loc = ct->ptr.token->loc ;
  86.  
  87.  
  88.         /*    
  89.          *    STA_STACK +:= STATE_STACK(n_STA_STACK + 1 ..
  90.          *                                    n_STATE_STACK - bk);
  91.          */
  92.  
  93.  
  94.         /*    Strip bk elements off the top off STATE_STACK    */
  95.  
  96.         while( bk-- > 0)
  97.             STATE_STACK = STATE_STACK->link ;
  98.  
  99.         /* Count the size of STATE_STACK */
  100.  
  101.         n_STATE_STACK = 0 ;
  102.         tmp_tp = STATE_STACK ;
  103.         while (tmp_tp != NULL) {
  104.             n_STATE_STACK ++ ;
  105.             tmp_tp = tmp_tp->link ;
  106.         }
  107.  
  108.         /* Count the size of STATE_STACK */
  109.  
  110.         n_sta_stack = 0 ;
  111.         tmp_tp = sta_stack ;
  112.         while (tmp_tp != NULL) {
  113.             n_sta_stack ++ ;
  114.             tmp_tp = tmp_tp->link ;
  115.         }
  116.  
  117.         /*    Put the top (n_STATE_STACK - n_sta_stack) elements of STATE_STACK
  118.          *  on the end of sta_stack
  119.          */
  120.          i = n_STATE_STACK - n_sta_stack ;
  121.          tmp_tp = STATE_STACK ;
  122.          while (--i > 0 )
  123.             tmp_tp = tmp_tp->link ;
  124.          tmp_tp->link = sta_stack ;
  125.          sta_stack = STATE_STACK ;
  126.  
  127.         reset_cands() ;
  128.         return;
  129.     }    /* if */
  130.  
  131.     /************************** S P E L L   S U B S T *********************/
  132.  
  133.     else if (CANDIDATES[SPELL_SUBST] != NULL ) {
  134.  
  135.  
  136.         /* [subtk, -, bk] := CANDIDATES[SPELL_SUBST]; */
  137.  
  138.         tv = namelist((subtk = CANDIDATES[SPELL_SUBST]->token_sym ));
  139.  
  140.         /* ct = bk == 0 ? curtok : prs_stack(n_prs_stack - bk + 1) ; */
  141.     
  142.         if ((bk = CANDIDATES[SPELL_SUBST]->backup_toks) == 0) {
  143.             ct = copytoken(curtok) ;
  144.         }
  145.         else {
  146.             /* ct := prs_stack(n_prs_stack - bk + 1) */ 
  147.             int    bktmp = bk ;
  148.             ct = prs_stack ;
  149.             while(--bktmp)
  150.                 ct = ct->prev ;
  151.             ct->prev = NULL ;
  152.         }
  153.         /* PRSERR_LOC := position(ct); */
  154.         PRSERR_LOC = ct->ptr.token->loc ;
  155.  
  156.         /* tokmod := [[subtk, tv, top(ct)]]; */
  157.         tokmod = ct ;
  158.         tokmod->prev = NULL ;
  159.         tokmod->symbol = subtk ;
  160.         /* tokmod->ptr.token->index = tv ; */
  161.         tokmod->ptr.token->loc = ct->ptr.token->loc ; 
  162.  
  163.         sprintf(msg , "Reserved word \"%s\" misspelled", tv ) ;
  164.         syntax_err(LLOC(ct),RLOC(ct),msg);
  165.     }
  166.  
  167.     /***************************** I N S E R T *******************************/
  168.  
  169.     else if (CANDIDATES[INSERT] != NULL ) {
  170.         
  171.         struct    tok_loc pt,ct ;
  172.         struct  prsstack *pt_token;
  173.         short        tmp_symbol ;
  174.  
  175.  
  176.         /* [intok, -, bk] := arb CANDIDATES[INSERT]; */
  177.  
  178.         tv = namelist((intok = CANDIDATES[INSERT]->token_sym));
  179.  
  180.         /* curtoken := bk == 0 ? curtok : prs_stack(n_prs_stack - bk + 1) ; */
  181.  
  182.         if ((bk = CANDIDATES[INSERT]->backup_toks) == 0) {
  183.             curtoken = copytoken(curtok) ;
  184.         }
  185.         else {
  186.             /* curtoken := prs_stack(n_prs_stack - bk + 1) */ 
  187.             int    bktmp = bk ;
  188.  
  189.             struct prsstack *tmp_ps = prs_stack ;
  190.  
  191.             while(--bktmp)
  192.                 tmp_ps = tmp_ps->prev ;
  193.  
  194.             pt =  r_span(tmp_ps->prev) ;
  195.             tmp_symbol = tmp_ps->prev->symbol ;
  196.             /* copytoken(curtoken,tmp_ps) ; */
  197.             curtoken = tmp_ps;
  198.             pt_token = curtoken->prev;
  199.         }
  200.  
  201.         if (bk == 0) {
  202.             /* pt =     r_span(prs_stack) ; */
  203.             /* if parse stack is empty (error on first token),
  204.                use location of current token  */
  205.             /* Metaware HIC does not like next line - so expand it manually 
  206.                 pt = (prs_stack != (struct prsstack *)0
  207.                   ? r_span(prs_stack)
  208.                 : l_span(curtoken) ) ;
  209.              */
  210.             if (prs_stack != (struct prsstack *)0) {
  211.                 pt = r_span(prs_stack);
  212.                 pt_token = prs_stack;
  213.             }
  214.             else {
  215.                 pt = l_span(curtoken);
  216.                 pt_token = curtoken;
  217.             }
  218.             tmp_symbol = prs_stack->symbol ;
  219.         }
  220.  
  221.         ct = l_span(curtoken) ;
  222.  
  223.         for (i = 1 ; i <= bk; i++ ) {
  224.             r = prs_stack;
  225.             prs_stack = prs_stack->prev ;
  226.             ADDTOTOP(r) ;
  227.         } 
  228.         /* 
  229.          *    tokmod := [curtoken, [intok, get_name(token_value_des(tv) 
  230.          *                    ? tv), top(pt)]];                             
  231.          */
  232.  
  233.         /* curtoken->prev = TALLOC() ; */
  234.         /* curtoken->prev->ptr->index = namemap(tv) ; */
  235.         /* curtoken->prev->ptr.token->loc = pt->ptr->loc ; */
  236.         tokens[tokind] = curtoken ;
  237.  
  238.         ADDTOTOP(PRSALLOC()) ;
  239.         tokens[tokind]->symbol = intok ;
  240.         tokens[tokind]->ptr.token = TOKALLOC() ;
  241.         tokens[tokind]->ptr.token->index = namemap((char *)token_val_des(tv),
  242.                                             strlen((char *)token_val_des(tv))) ;
  243.         tokens[tokind]->ptr.token->loc = pt ;
  244.         tokmod = NULL;        
  245.  
  246.  
  247.         if (n_CANDIDATES[INSERT] == 1) {
  248.  
  249.             if (( tmp_symbol == SEMI_SYM  )
  250.               || (prs_stack == NULL) /* if parse stack empty */
  251.               || ( tmp_symbol > EOFT_SYM ) ) {
  252.                 /*
  253.                 * This kludge is to take
  254.                 * care of nonterminals
  255.                 */
  256.                 sprintf(msg,"\"%s\" expected before this token",tv);
  257.                 /*
  258.                 syntax_err(&PREVTOK->ptr.token->loc,
  259.                   &PREVTOK->ptr.token->loc,msg);
  260.                 */
  261.                 syntax_err(LLOC(curtoken),RLOC(curtoken),msg) ;
  262.                 PRSERR_LOC = ct ;
  263.             }
  264.             else {
  265.                 PRSERR_LOC = pt ;
  266.                 sprintf(msg,"\"%s\" expected after this token",tv);
  267.                 syntax_err(LLOC(pt_token),RLOC(pt_token),msg) ;
  268.                 /*
  269.                 syntax_err(&PREVTOK->ptr.token->loc,
  270.                   &PREVTOK->ptr.token->loc,msg);
  271.                 syntax_err(&curtok->ptr.token->loc,&curtok->ptr.token->loc,msg);
  272.                 */
  273.             }
  274.         }
  275.         else {
  276.             PCAND    next_cand ;
  277.             char *tmp = msg;
  278.             /* 
  279.              * errfile := [
  280.              *     '{' +/[' ' + NAME_LIST(x(1)) : x in inserts] 
  281.              *            + ' } expected after this token'];
  282.              */
  283.  
  284.             sprintf(tmp,"One of { ");
  285.             tmp += strlen(tmp);
  286.             next_cand = CANDIDATES[INSERT] ;
  287.             while(next_cand != NULL) {    
  288.                 sprintf(tmp,"%s ",namelist(next_cand->token_sym) ) ;
  289.                 tmp += strlen(tmp);
  290.                 next_cand = next_cand->next ;
  291.             }
  292.  
  293.             sprintf(tmp," } expected after this token");
  294.             PRSERR_LOC = pt ;
  295.             syntax_err(LLOC(pt_token),RLOC(pt_token),msg);
  296.         }
  297.  
  298.         /* 
  299.          * STA_STACK +:= STATE_STACK(n_STA_STACK + 1 .. n_STATE_STACK - bk) ; 
  300.          */
  301.  
  302.         /*    Strip bk elements off the top off STATE_STACK    */
  303.  
  304.         while(bk--)
  305.             STATE_STACK = STATE_STACK->link ;
  306.  
  307.         /* Count the size of STATE_STACK */
  308.  
  309.         n_STATE_STACK = 0 ;
  310.         tmp_tp = STATE_STACK ;
  311.         while (tmp_tp != NULL) {
  312.             n_STATE_STACK ++ ;
  313.             tmp_tp = tmp_tp->link ;
  314.         }
  315.  
  316.         /* Count the size of STATE_STACK */
  317.  
  318.         n_sta_stack = 0 ;
  319.         tmp_tp = sta_stack ;
  320.         while (tmp_tp != NULL) {
  321.             n_sta_stack ++ ;
  322.             tmp_tp = tmp_tp->link ;
  323.         }
  324.  
  325.         /*    Put the top (n_STATE_STACK - n_sta_stack) elements of STATE_STACK
  326.          *  on the end of sta_stack
  327.          */
  328.          i = n_STATE_STACK - n_sta_stack ;
  329.          tmp_tp = STATE_STACK ;
  330.          while (--i > 0 )
  331.             tmp_tp = tmp_tp->link ;
  332.          tmp_tp->link = sta_stack ;
  333.          sta_stack = STATE_STACK ;
  334.  
  335.         reset_cands() ;
  336.         return ;
  337.  
  338.     } /* if */
  339.  
  340.     /**************************** D E L E T E *********************************/
  341.  
  342.     else if (CANDIDATES[DELETE] != NULL ) {
  343.  
  344.         /* [-, bk] := arb CANDIDATES[DELETE];                         */
  345.         /* ct = bk == 0 ? curtok : prs_stack(n_prs_stack - bk + 1) ; */
  346.  
  347.         if ((bk = CANDIDATES[DELETE]->backup_toks) == 0)
  348.             ct = copytoken(curtok) ;
  349.         else {
  350.             /* ct := prs_stack(n_prs_stack - bk + 1) ;*/ 
  351.             int    bktmp = bk ;
  352.             struct prsstack *tmp_ps = prs_stack ;
  353.  
  354.             while(--bktmp)
  355.                 tmp_ps = tmp_ps->prev ;
  356.  
  357.             ct = copytoken(tmp_ps) ;
  358.         }
  359.  
  360.         sprintf(msg, "Unexpected \"%s\" ignored", find_name(ct) ) ;
  361.         PRSERR_LOC = ct->ptr.token->loc ;
  362.         syntax_err(LLOC(ct),RLOC(ct),msg);
  363.  
  364.         tokmod = NULL ;
  365.         delete_flag = 1;
  366.     } /* if */
  367.  
  368.     /************************** S U B S T ************************************/
  369.  
  370.     else if (CANDIDATES[SUBST] != NULL) {
  371.  
  372.         /* [subtk, -, bk] := arb substs; */
  373.  
  374.         tv = namelist((subtk = CANDIDATES[SUBST]->token_sym));
  375.  
  376.         /* ct = bk == 0 ? curtok : prs_stack(n_prs_stack - bk + 1) ; */
  377.  
  378.         if ((bk = CANDIDATES[SUBST]->backup_toks) == 0)
  379.             ct = copytoken(curtok) ;
  380.         else {
  381.             /* ct := prs_stack(n_prs_stack - bk + 1) */
  382.             int    bktmp = bk ;
  383.             struct prsstack *tmp_ps = prs_stack ;
  384.  
  385.             while(--bktmp)
  386.                 tmp_ps = tmp_ps->prev ;
  387.  
  388.             ct = copytoken(tmp_ps) ;
  389.         }
  390.  
  391.         PRSERR_LOC = ct->ptr.token->loc ;
  392.  
  393.         /* 
  394.          * tokmod := [[subtk, get_name(token_value_des(tv) ? tv), top(ct)]];
  395.          */
  396.         tokmod = copytoken(ct) ;
  397.         tokmod->prev = NULL ;
  398.         tokmod->symbol = subtk ;
  399.         tokmod->ptr.token->index = namemap(token_val_des(tv),
  400.           strlen(token_val_des(tv))) ; 
  401.  
  402.         if (n_CANDIDATES[SUBST] == 1) {
  403.             if ( (is_reserved(CANDIDATES[SUBST]->token_sym) )
  404.               && (ct->symbol == ID_SYM) && (spell(tv,find_name(ct) ) < 3) )
  405.                 sprintf(msg,"Reserved word \"%s\" misspelled", tv) ;
  406.             else
  407.                 sprintf(msg,"\"%s\" expected instead of \"%s\"",
  408.                   tv,find_name(ct) ) ;
  409.  
  410.             syntax_err(LLOC(ct),RLOC(ct),msg);
  411.         }
  412.         else {
  413.             char    *tmp = msg;
  414.             PCAND    next_cand ;
  415.             /*
  416.              * errfile := ['{' +/[' ' + NAME_LIST(x(1)) : x in substs]
  417.              *                + ' } expected instead of ' + '"' +
  418.              *                find_name(ct) + '"'];
  419.              */
  420.             sprintf(tmp,"One of { ");
  421.             tmp += strlen(tmp);
  422.             next_cand = CANDIDATES[SUBST] ;
  423.             while(next_cand != NULL) {    
  424.                 sprintf(tmp,"%s ",namelist(next_cand->token_sym) ) ;
  425.                 tmp += strlen(tmp);
  426.                 next_cand = next_cand->next ;
  427.             }
  428.             sprintf(tmp," } expected instead of \"%s\"",find_name(ct)) ;
  429.             syntax_err(LLOC(ct),RLOC(ct),msg);
  430.         }
  431.     } 
  432.  
  433.     /*
  434.      * Alter token sequence in accordance with corrective action.
  435.      * Restore state and parse stacks to correction point.
  436.      */
  437.  
  438.     for (i = 1 ; i<=bk; i++ ) {
  439.         r = prs_stack;
  440.         prs_stack = prs_stack->prev ;
  441.         r->prev = NULL ;
  442.         ADDTOTOP(r) ;
  443.     } 
  444.  
  445.     if (tokmod != NULL) 
  446.         tokens[tokind] = tokmod ;
  447.     else if (delete_flag)
  448.         TOKMINUS;
  449.     /* 
  450.      * STA_STACK +:= STATE_STACK(n_STA_STACK + 1 .. n_STATE_STACK - bk) ; 
  451.      */
  452.  
  453.     /*    Strip bk elements off the top off STATE_STACK    */
  454.  
  455.     while (bk--)
  456.         STATE_STACK = STATE_STACK->link ;
  457.  
  458.     /* Count the size of STATE_STACK */
  459.  
  460.     n_STATE_STACK = 0 ;
  461.     tmp_tp = STATE_STACK ;
  462.     while (tmp_tp != NULL) {
  463.         n_STATE_STACK ++ ;
  464.         tmp_tp = tmp_tp->link ;
  465.     }
  466.  
  467.     /* Count the size of STATE_STACK */
  468.  
  469.     n_sta_stack = 0 ;
  470.     tmp_tp = sta_stack ;
  471.     while (tmp_tp != NULL) {
  472.         n_sta_stack ++ ;
  473.         tmp_tp = tmp_tp->link ;
  474.     }
  475.  
  476.     /*
  477.      *    Put the top (n_STATE_STACK - n_sta_stack) elements of STATE_STACK
  478.      *  on the end of sta_stack
  479.      */
  480.      i = n_STATE_STACK - n_sta_stack ;
  481.      tmp_tp = STATE_STACK ;
  482.      while (--i > 0 )
  483.         tmp_tp = tmp_tp->link ;
  484.      tmp_tp->link = sta_stack ;
  485.      sta_stack = STATE_STACK ;
  486.  
  487.      reset_cands() ;
  488.  
  489. }    /* end procedure make_correction; */
  490.  
  491. static void reset_cands()                                    /*;reset_cands*/
  492. {
  493.     int i ;
  494.     /* 
  495.      * Reset the sets of candidates to empty for future errors 
  496.      */
  497.      for (i = DELETE ; i <= SPELL_SUBST ; i ++ ) 
  498.      {
  499.         n_CANDIDATES[i] = 0 ;
  500.         CANDIDATES[i] = NULL ;
  501.      }
  502. }
  503.